package pm

import (
	"context"
	"encoding/json"
	"ez/apps/pm/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type FeeHouseOrder struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64       `json:"id" bson:"id,omitempty"`
	Sn          string      `json:"sn" bson:"sn"`               //订单编号
	PayType     *PayType    `json:"payType" bson:"payType"`     //付款方式
	PayTypeId   int64       `json:"payTypeId" bson:"payTypeId"` //付款方式
	Amount      float64     `json:"amount" bson:"amount"`       //费用金额
	Cut         float64     `json:"cut" bson:"cut"`             //优惠金额
	Note        string      `json:"note" bson:"note"`           //备注
	State       int64       `json:"state" bson:"state"`         //状态
	Bills       []*FeeHouse `json:"bills" bson:"bills"`         //物业费账单
	CreateAt    time.Time   `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time   `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time  `json:"deleteAt" bson:"deleteAt"`
}

func (this *FeeHouseOrder) DocName() string { return "FeeHouseOrder" }
func (this *FeeHouseOrder) GetId() int64    { return this.Id }
func (this *FeeHouseOrder) SetId(id int64)  { this.Id = id }
func (this *FeeHouseOrder) Create() error {
	return this.GetFactory().Create(this)
}
func (this *FeeHouseOrder) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *FeeHouseOrder) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *FeeHouseOrder) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *FeeHouseOrder) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *FeeHouseOrder) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *FeeHouseOrder) ToString() string {
	return string(this.ToBytes())
}
func (this *FeeHouseOrder) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *FeeHouseOrder) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *FeeHouseOrder) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *FeeHouseOrder) LoadPayType() {
	if this.PayTypeId == 0 {
		return
	}
	this.PayType, _ = NewPayTypeCrud().FindId(this.PayTypeId)
}
func (this *FeeHouseOrder) LoadBills() {
	bills, _ := NewFeeHouseCrud().FindBy(ss.M{"orderId": this.Id}, []string{"id"}, 0, 0)
	this.Bills = bills
}
func (this *FeeHouseOrder) ClearRelationsBeforeSave() mgo.Doc {
	this.PayType = nil
	this.Bills = nil
	return this
}
func neverUsed_FeeHouseOrder() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type FeeHouseOrderAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *FeeHouseOrder
	Session *ez.Session
}

func NewFeeHouseOrderAccessControl(model *FeeHouseOrder, action string, session *ez.Session) *FeeHouseOrderAccessControl {
	ctrl := &FeeHouseOrderAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.FeeHouseOrderAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
